package club.beimeng.On2.SelectionSort;

import java.util.Arrays;

public class SelectionSort {
    // 选择排序
    public static void sort1(int[] nums){
        int minIndex = 0;
        for(int i=0;i<nums.length-1;i++){
            minIndex = i;
            for(int j= minIndex;j<nums.length;j++){
                if (nums[minIndex] > nums[j]){
                    minIndex = j;
                }
            }
            int temp = nums[minIndex];
            nums[minIndex] = nums[i];
            nums[i] = temp;
        }
    }

    public static void sort2(int[] nums){
        int minIndex,maxIndex;
        for (int i = 0; i < nums.length / 2; i++) {
            minIndex = i;
            maxIndex = i;
            for (int j = i+1; j < nums.length - i; j++) {
                if (nums[minIndex] > nums[j]){
                    minIndex = j;
                }
                if (nums[maxIndex] < nums[j]){
                    maxIndex = j;
                }
            }
            if (minIndex == maxIndex) break;
            int temp = nums[i];
            nums[i] = nums[minIndex];
            nums[minIndex] = temp;
            if (maxIndex == i)maxIndex = minIndex;
            int lastIndex = nums.length-1-i;
            temp = nums[lastIndex];
            nums[lastIndex] = nums[maxIndex];
            nums[maxIndex] = temp;
        }
    }

    public static void main(String[] args) {
        int[] nums = {2,3,41,4,6,8,9,0,123,2};
        SelectionSort.sort2(nums);
        System.out.println("nums = " + Arrays.toString(nums));
    }
}
